package linwen.wang.tdddemo.service;

import linwen.wang.tdddemo.common.UserInfoIsNullException;
import linwen.wang.tdddemo.entity.UserInfoEntity;
import linwen.wang.tdddemo.vo.UserInfo;
import linwen.wang.tdddemo.repository.UserRepository;
import linwen.wang.tdddemo.utils.PhoneNumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserInfoService {
    @Autowired
    private UserRepository userRepository;

    public UserInfo getUserInfo(Long id){
        UserInfoEntity userInfo = userRepository.getUserInfo(id);
        if (userInfo == null){
            throw new UserInfoIsNullException("用户信息不存在");
        }
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setId(userInfo.getId());
        userInfo1.setName(userInfo.getName());
        userInfo1.setPhoneNumber(userInfo.getPhoneNumber());
        userInfo1.setPhoneNumberCity(PhoneNumberUtils.getCityIdByPhoneNumber(userInfo.getPhoneNumber()));
        return userInfo1;
    }

    public void saveUserInfo(UserInfo userInfo){
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setPhoneNumber(userInfo.getPhoneNumber());
        userInfoEntity.setName(userInfo.getName());
        userRepository.save(userInfoEntity);
    }
}
